<!DOCTYPE html>
<html lang="en-us">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    
    <meta property="og:site_name" content="TinyKE小客同学">
    <meta property="og:type" content="article">

    
    <meta property="og:image" content="https://coldke.gitee.io/blog/img/home-bg-motor.jpg">
    <meta property="twitter:image" content="https://coldke.gitee.io/blog/img/home-bg-motor.jpg" />
    

    
    <meta name="title" content="lighttpd的匹配表达式" />
    <meta property="og:title" content="lighttpd的匹配表达式" />
    <meta property="twitter:title" content="lighttpd的匹配表达式" />
    

    
    <meta name="description" content="介绍lighttpd中的表达式特殊匹配">
    <meta property="og:description" content="介绍lighttpd中的表达式特殊匹配" />
    <meta property="twitter:description" content="介绍lighttpd中的表达式特殊匹配" />
    

    
    <meta property="twitter:card" content="summary" />
    
    

    <meta name="keyword"  content="小客, 小客的网络日志, 小客的博客, TinyKE Blog, 博客, 个人网站, 互联网, Web, 云原生, PaaS, Istio, Kubernetes, 微服务, Microservice">
    <link rel="shortcut icon" href="/blog/img/favicon.ico">

    <title>lighttpd的匹配表达式 | 小客的博客 | TinyKE Blog</title>

    <link rel="canonical" href="/blog/2022-05/reg-in-lighttpd/">

    
    
    
    <link rel="stylesheet" href="/blog/css/bootstrap.min.css">

    
    <link rel="stylesheet" href="/blog/css/hugo-theme-cleanwhite.min.css">

    
    <link rel="stylesheet" href="/blog/css/zanshang.css">

    
    <link href="https://cdn.jsdelivr.net/gh/FortAwesome/Font-Awesome@5.15.1/css/all.css" rel="stylesheet" type="text/css">

    
    

    
    <script src="/blog/js/jquery.min.js"></script>

    
    <script src="/blog/js/bootstrap.min.js"></script>

    
    <script src="/blog/js/hux-blog.min.js"></script>

    
    

</head>




<nav class="navbar navbar-default navbar-custom navbar-fixed-top">

    <div class="container-fluid">
        
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/blog/">TinyKE小客同学</a>
        </div>

        
        
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/blog/">All Posts</a>
                    </li>
                    
                        
                        <li>
                            <a href="/blog/categories/tech">tech</a>
                        </li>
                        
                    
                    
		    
                        <li><a href="/blog/top/books/">BOOKS</a></li>
                    
                        <li><a href="/blog/top/archive/">ARCHIVE</a></li>
                    
                        <li><a href="/blog/top/about/">ABOUT</a></li>
                    

                    
		    <li>
                        <a href="/blog/search"><i class="fa fa-search"></i></a>
		    </li>
                    
                </ul>
            </div>
        </div>
        
    </div>
    
</nav>
<script>
    
    
    
    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        
            $navbar.className = " ";
            
            setTimeout(function(){
                
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>




<style type="text/css">
    header.intro-header {
        background-image: url('/blog/img/home-bg-motor.jpg')
    }
</style>

<header class="intro-header" >

    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <div class="tags">
                        
                        <a class="tag" href="/blog/tags/lighttpd" title="lighttpd">
                            lighttpd
                        </a>
                        
                    </div>
                    <h1>lighttpd的匹配表达式</h1>
                    <h2 class="subheading">lighttpd中相关的表达式使用</h2>
                    <span class="meta">
                        
                            Posted by 
                            
                                    &#34;TinyKE&#34;
                             
                            on 
                            Wednesday, May 4, 2022
                            
                            
                            
                            
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>




<article>
    <div class="container">
        <div class="row">

            
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                post-container">

                
                <h2 id="lighttpd中的匹配表达式">lighttpd中的匹配表达式</h2>
<h3 id="一表达式含义">一、表达式含义</h3>
<table>
<thead>
<tr>
<th>.</th>
<th style="text-align:left">匹配任意字符</th>
</tr>
</thead>
<tbody>
<tr>
<td>*</td>
<td style="text-align:left">匹配0或N个前置位字符</td>
</tr>
<tr>
<td>+</td>
<td style="text-align:left">匹配1或N个前置位字符</td>
</tr>
<tr>
<td>?</td>
<td style="text-align:left">匹配0或1个前置位字符</td>
</tr>
<tr>
<td>?</td>
<td style="text-align:left">匹配特别符号，比如：\. 匹配.字符</td>
</tr>
<tr>
<td>^</td>
<td style="text-align:left">匹配字符串开始</td>
</tr>
<tr>
<td>$</td>
<td style="text-align:left">匹配字符串末尾</td>
</tr>
<tr>
<td>[set]</td>
<td style="text-align:left">匹配中括号内的任一字符</td>
</tr>
<tr>
<td>[^set]</td>
<td style="text-align:left">匹配非中括号内的任一字符</td>
</tr>
<tr>
<td>（pattern）</td>
<td style="text-align:left">组，标记为一个特殊的匹配参数</td>
</tr>
<tr>
<td>{n,m}</td>
<td style="text-align:left">匹配前置位字符n到m次，m&gt;=n</td>
</tr>
<tr>
<td>(?!expression)</td>
<td style="text-align:left">匹配任意字符除了当前表达式</td>
</tr>
</tbody>
</table>
<h3 id="二和的参数调用1">二、%和$的参数调用<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup></h3>
<p>1、如果表达式中含有表达式匹配组，那么$1~$9分别代表了命中字符的1~9组，比如：</p>
<pre><code>eg:$1 代表匹配的第一个组
</code></pre><p>%替换符只在rewrite*中有意义，在evhost.path-pattern中是没有意义的，特别的是</p>
<p>如果rewrite*被放置于一个匹配条件中的话，%代表的是条件中的表达式，%1代表第一个子匹配，依次类推，其中%0代表整个匹配的字符串。</p>
<h3 id="三扩展匹配字符串">三、扩展匹配字符串</h3>
<blockquote>
<p>In addition to %1 and $1, the following modifiers are now supported, followed by the number for the backreference, e.g. ${esc:1}.</p>
<p>${noesc:&hellip;} no escaping
${esc:&hellip;} escape all non-alphanumeric - . _ ~ incl double-escape %
${escape:&hellip;} escape all non-alphanumeric - . _ ~ incl double-escape %
${escnde:&hellip;} escape all non-alphanumeric - . _ ~ but no double-esc %
${escpsnde:&hellip;} escape all non-alphanumeric - . _ ~ / but no double-esc % (preserves literal /)
${tolower:&hellip;}
${toupper:&hellip;}
${encb64u:&hellip;} encode to base64url characters (no-padding)
${decb64u:&hellip;} decode from base64url characters
%{noesc:&hellip;}
%{esc:&hellip;}
%{escape:&hellip;}
%{escnde:&hellip;}
%{escpsnde:&hellip;}
%{tolower:&hellip;}
%{toupper:&hellip;}
%{encb64u:&hellip;}
%{decb64u:&hellip;}
lighttpd provides ways to substitute URI parts without needing a regex match (since 1.4.50) (and can be preceded by encoding modifier, e.g. ${tolower:url.authority})</p>
<p>${url.scheme}
${url.authority}
${url.port}
${url.path}
${url.query}
${qsa} appends query string, if not empty</p>
</blockquote>
<h3 id="举例">举例</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#6272a4"># the following example, is, however just simulating vhost by rewrite</span>
<span style="color:#6272a4"># * you can never change document-root by mod_rewrite</span>
<span style="color:#6272a4"># use mod_*host instead to make real mass-vhost</span>

server.document-root <span style="color:#ff79c6">=</span> <span style="color:#f1fa8c">&#34;/www/htdocs/&#34;</span> 
<span style="color:#8be9fd;font-style:italic">$HTTP</span><span style="color:#ff79c6">[</span><span style="color:#f1fa8c">&#34;host&#34;</span><span style="color:#ff79c6">]</span> <span style="color:#ff79c6">=</span>~ <span style="color:#f1fa8c">&#34;^.*\.([^.]+\.com)</span>$<span style="color:#f1fa8c">&#34;</span> <span style="color:#ff79c6">{</span>
  url.rewrite-once <span style="color:#ff79c6">=</span> <span style="color:#ff79c6">(</span> <span style="color:#f1fa8c">&#34;^/(.*)&#34;</span> <span style="color:#ff79c6">=</span>&gt; <span style="color:#f1fa8c">&#34;/%0/</span><span style="color:#8be9fd;font-style:italic">$1</span><span style="color:#f1fa8c">&#34;</span> <span style="color:#ff79c6">)</span>
<span style="color:#ff79c6">}</span>

<span style="color:#6272a4"># request:        http://any.domain.com/url/ </span>
<span style="color:#6272a4"># before rewrite: REQUEST_URI=&#34;/www/htdocs/url/&#34; </span>
<span style="color:#6272a4"># and DOCUMENT_ROOT=&#34;/www/htdocs/&#34; %0=&#34;any.domain.com&#34; $1=&#34;url/&#34; </span>
<span style="color:#6272a4"># after rewrite:  REQUEST_URI=&#34;/www/htdocs/any.domain.com/url/&#34; </span>
<span style="color:#6272a4"># still, you have DOCUMENT_ROOT=/www/htdocs/</span>

<span style="color:#6272a4"># please note, that we have two regular expressions: the one which </span>
<span style="color:#6272a4"># $HTTP[&#34;host&#34;] is been compared with, and the one of the rewrite rule.</span>
<span style="color:#6272a4"># the numbered subexpressions available to build the relative uri are</span>
<span style="color:#6272a4"># being prefixed by &#39;%&#39; for subexpressions of the first regular expression </span>
<span style="color:#6272a4"># match and by &#39;$&#39; for subexpressions of the second one.</span>
<span style="color:#6272a4"># subexpression 0 interpolates the whole matching string: %0 for the whole</span>
<span style="color:#6272a4"># string matching the conditional, and $0 for the whole string matching the</span>
<span style="color:#6272a4"># rewrite rule.</span>

<span style="color:#6272a4"># if the rewrite rule is not included in a conditional </span>
<span style="color:#6272a4"># block, only the &#39;$&#39; prefixed variables are available.</span>

url.rewrite-once <span style="color:#ff79c6">=</span> <span style="color:#ff79c6">(</span> <span style="color:#f1fa8c">&#34;^/id/([0-9]+)</span>$<span style="color:#f1fa8c">&#34;</span> <span style="color:#ff79c6">=</span>&gt; <span style="color:#f1fa8c">&#34;/index.php?id=</span><span style="color:#8be9fd;font-style:italic">$1</span><span style="color:#f1fa8c">&#34;</span>,
                     <span style="color:#f1fa8c">&#34;^/link/([a-zA-Z]+)&#34;</span> <span style="color:#ff79c6">=</span>&gt; <span style="color:#f1fa8c">&#34;/index.php?link=</span><span style="color:#8be9fd;font-style:italic">$1</span><span style="color:#f1fa8c">&#34;</span> <span style="color:#ff79c6">)</span>
</code></pre></div><section class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1" role="doc-endnote">
<p><a href="https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModRewrite#Regular-Expressions">https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModRewrite#Regular-Expressions</a> <a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</section>


                

                <hr>
                <ul class="pager">
                    
                    <li class="previous">
                        <a href="/blog/post/readme/" data-toggle="tooltip" data-placement="top" title="READ ME">&larr;
                            Previous Post</a>
                    </li>
                    
                    
                    <li class="next">
                        <a href="/blog/2022-05/intel-IGD-passthrough-in-PVE/" data-toggle="tooltip" data-placement="top" title="intel IGD passthrough">Next
                            Post &rarr;</a>
                    </li>
                    
                </ul>
                

            </div>

            
            
            <div class="
                col-lg-2 col-lg-offset-0
                visible-lg-block
                sidebar-container
                catalog-container">
                <div class="side-catalog">
                    <hr class="hidden-sm hidden-xs">
                    <h5>
                        <a class="catalog-toggle" href="#">CATALOG</a>
                    </h5>
                    <ul class="catalog-body"></ul>
                </div>
            </div>
            

            
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                
                
                <section>
                    <hr class="hidden-sm hidden-xs">
                    <h5><a href="/tags/">FEATURED TAGS</a></h5>
                    <div class="tags">
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                    </div>
                </section>
                

                
                
            </div>
        </div>
    </div>
</article>




<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">                  
                    
                    <li>
                        <a href="mailto:jnee520@gmail.com">
                            <span class="fa-stack fa-lg">
                                <i class="fas fa-circle fa-stack-2x"></i>
                                <i class="fas fa-envelope fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
		           
                    
                    
                    
                    

		            
                    
                    <li>
                        <a target="_blank" href="/blog/your%20wechat%20qr%20code%20image">
                            <span class="fa-stack fa-lg">
                                <i class="fas fa-circle fa-stack-2x"></i>
                                <i class="fab fa-weixin fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
		            
                    
                    
                    
                    
                    <li>
                        <a target="_blank" href="https://www.linkedin.com/in/yourlinkedinid">
                            <span class="fa-stack fa-lg">
                                <i class="fas fa-circle fa-stack-2x"></i>
                                <i class="fab fa-linkedin fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
		           
                    
                    
                    <li>
                        <a target="_blank" href="https://stackoverflow.com/users/yourstackoverflowid">
                            <span class="fa-stack fa-lg">
                                <i class="fas fa-circle fa-stack-2x"></i>
                                <i class="fab fa-stack-overflow fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    
                    
                    
            
            
            
           
                   <li>
                       <a href='' rel="alternate" type="application/rss+xml" title="TinyKE小客同学" >
                           <span class="fa-stack fa-lg">
                               <i class="fas fa-circle fa-stack-2x"></i>
                               <i class="fas fa-rss fa-stack-1x fa-inverse"></i>
                           </span>
                       </a>
                   </li>
            
             </ul>
		<p class="copyright text-muted">
                    Copyright &copy; TinyKE小客同学 2022
                    <br>
                    <a href="https://themes.gohugo.io/hugo-theme-cleanwhite">CleanWhite Hugo Theme</a> by <a href="https://zhaohuabing.com">Huabing</a> |
                    <iframe
                        style="margin-left: 2px; margin-bottom:-5px;"
                        frameborder="0" scrolling="0" width="100px" height="20px"
                        src="https://ghbtns.com/github-btn.html?user=zhaohuabing&repo=hugo-theme-cleanwhite&type=star&count=true" >
                    </iframe>
                </p>
            </div>
        </div>
    </div>
</footer>




<script>
    function loadAsync(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>






<script>
    
    if($('#tag_cloud').length !== 0){
        loadAsync("/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>


<script>
    loadAsync("https://cdn.jsdelivr.net/npm/fastclick@1.0.6/lib/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>









<script type="text/javascript">
    function generateCatalog(selector) {

        
        
        
        
            _containerSelector = 'div.post-container'
        

        
        var P = $(_containerSelector), a, n, t, l, i, c;
        a = P.find('h1,h2,h3,h4,h5,h6');

        
        $(selector).html('')

        
        a.each(function () {
            n = $(this).prop('tagName').toLowerCase();
            i = "#" + $(this).prop('id');
            t = $(this).text();
            c = $('<a href="' + i + '" rel="nofollow">' + t + '</a>');
            l = $('<li class="' + n + '_nav"></li>').append(c);
            $(selector).append(l);
        });
        return true;
    }

    generateCatalog(".catalog-body");

    
    $(".catalog-toggle").click((function (e) {
        e.preventDefault();
        $('.side-catalog').toggleClass("fold")
    }))

    


    loadAsync("\/blog\/js\/jquery.nav.js", function () {
        $('.catalog-body').onePageNav({
            currentClass: "active",
            changeHash: !1,
            easing: "swing",
            filter: "",
            scrollSpeed: 700,
            scrollOffset: 0,
            scrollThreshold: .2,
            begin: null,
            end: null,
            scrollChange: null,
            padding: 80
        });
    });
</script>





</body>
</html>
